package aplicacion.vista;

import aplicacion.modelo.Poligono;

/**
 *
 * @author Ileana Natalia Díaz Palacios
 * Programación III
 * Universidad Nacional
 * 
 */
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

public class PanelDibujo extends JPanel {

    public PanelDibujo() {
        super(true);
        setBackground(Color.WHITE);
        poligonos = new ArrayList<Poligono>();
    }

    public void agregarPoligono(Poligono p) {
        poligonos.add(p);
        updateUI();
    }

    @Override
    public void paintComponent(Graphics gb) {
        super.paintComponent(gb);
        Graphics2D g = (Graphics2D) gb;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.DARK_GRAY);
        g.setFont(TIPO_POR_OMISION);
        //g.drawString(String.format("paint(), k = %d", k++), 16, 24);
        //    ArrayList<Poligono> poligonos = ManejadorCargaPoligonos.obtenerInstancia().cargarXML("C:\\Users\\Chris\\Documents\\NetBeansProjects\\trunk\\PoligonoMaker\\src\\aplicacion\\modelo\\gestores\\poligonos.xml");
        for (Poligono p : poligonos) {
            g.drawPolygon(p);
        }

    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    @Override
    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, this);
            setOpaque(false);
        }
        super.paint(g);
    }

    public void borrarTodo() {
        poligonos.clear();
        if (image != null) {
            image = null;
        }
        updateUI();
    }

    public void borrarUltimo() {
        if (!poligonos.isEmpty()) {
            poligonos.remove(poligonos.size() - 1);
        }
        if (image != null) {
            image = null;
        }
        updateUI();
    }
    private Image image = null;
    private ArrayList<Poligono> poligonos = null;
    private long k = 0;
    private static final Font TIPO_POR_OMISION = new Font("Arial,", Font.PLAIN, 12);
}
